package ru.st.selenium.applogicImp;

import org.openqa.selenium.WebDriver;

import ru.st.selenium.applogicTop.ApplicationManager;
import ru.st.selenium.applogicTop.EditFieldsTypesObjectHelper;
import ru.st.selenium.applogicTop.TypesObjectHelper;
import ru.st.selenium.applogicTop.NavigationHelper;
import ru.st.selenium.applogicTop.UserHelper;
import ru.st.selenium.applogicTop.UsersAndDepartmentsHelper;
import ru.st.selenium.util.Browser;
import ru.st.selenium.util.PropertyLoader;
import ru.st.selenium.webdriver.WebDriverFactory;

public class ApplicationManagerImp implements ApplicationManager {

	private UserHelper userHelper;
	private TypesObjectHelper typesObjectHelper;
	private NavigationHelper navHelper;
	private EditFieldsTypesObjectHelper editFieldsTTHelper;
	private UsersAndDepartmentsHelper usersAndDepartmentsHelper;

	private WebDriver driver;
	private String baseUrl;

	public ApplicationManagerImp() {
		baseUrl = PropertyLoader.loadProperty("site.url");
		String gridHubUrl = PropertyLoader.loadProperty("grid2.hub");

		Browser browser = new Browser();
		browser.setName(PropertyLoader.loadProperty("browser.name"));
		browser.setVersion(PropertyLoader.loadProperty("browser.version"));
		browser.setPlatform(PropertyLoader.loadProperty("browser.platform"));

		String username = PropertyLoader.loadProperty("user.username");
		String password = PropertyLoader.loadProperty("user.password");

		driver = WebDriverFactory.getInstance(gridHubUrl, browser, username,
				password);

		userHelper = new UserHelperImp(this);
		typesObjectHelper = new TypesObjectHelperImp(this);
		navHelper = new NavigationHelperImp(this);
		editFieldsTTHelper = new EditFieldsTypesObjectHelperImp(this);
		usersAndDepartmentsHelper = new UsersAndDepartmentsHelperImp(this);
		
		getNavigationHelper().openMainPage();
	}

	@Override
	public UserHelper getUserHelper() {
		return userHelper;
	}

	@Override
	public TypesObjectHelper getTypesObjectHelper() {
		return typesObjectHelper;
	}

	@Override
	public NavigationHelper getNavigationHelper() {
		return navHelper;
	}

	@Override
	public EditFieldsTypesObjectHelper getEditFieldsTypesObjectHelper() {
		return editFieldsTTHelper;
	}
	
	@Override
	public UsersAndDepartmentsHelper getUsersAndDepartmentsHelper() {
		return usersAndDepartmentsHelper;
	}
	
	
	protected WebDriver getWebDriver() {
		return driver;
	}

	protected String getBaseUrl() {
		return baseUrl;
	}

	@Override
	public void stop() {
		if (driver != null) {
			driver.quit();
		}
	}


}
